<template>
  <el-container>
<!--    头部区域-->
    <el-header>
      <div class="homeHeader">
        <img src="../assets/logo.png" alt="">
        <span class="headerSpan">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
<!--    内容主体区域-->
    <el-container>
<!--      左侧菜单栏-->
      <el-aside :width="isCollapse? '64px':'200px'">
<!--        展开折叠按钮-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409FFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath">
<!--          default-active 当前激活的index-->
<!--          unique-opened是否只保持一个子菜单的展开,默认为false-->
<!--          router 开启路由模式以 index 作为 path 进行路由跳转-->
<!--          一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
<!--            一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
<!--            二级菜单-->
<!--            :idnex="subItem.path 作为路由的跳转地址"-->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
<!--                二级菜单的模板区域-->
                <template slot="title">
<!--                  图标-->
                  <i class="el-icon-menu"></i>
<!--                  文本-->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
<!--      右侧内容-->
      <el-main>
<!--        路由占位符-->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
    //  获取的数据挂载到data中
      menulist: [],
      //  菜单列表对象
      iconObj: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli1',
        145: 'iconfont icon-caidanshujutongji-copy-copy-copy'
      },
      // 默认不折叠菜单栏
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  // 获取左侧菜单
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出
    loginOut () {
      // 清空token，跳转到登录首页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 把data数据保存到menulist中
      this.menulist = res.data
    },
    //  控制菜单栏的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 点击菜单保存到sessionStorage
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      //  重新赋值
      this.activePath = activePath
    },
    handleSelect (activePath) { }
  }
}
</script>
<style scoped>
  .iconfont {
    margin-right: 10px;
  }
  .el-container {
    height: 100%;
  }
  .el-header {
    background-color: #373f41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
  }
  .homeHeader {
    display: flex;
  }
  img {
    height: 40px;
  }
  .headerSpan {
    display: flex;
    align-items: center;
    padding-left: 15px;
  }
  .el-aside {
    background-color: #333744;
  }
  .el-menu {
    border-right: 0;
  }
  .el-main {
    background-color: #eaedf1;
  }
  .toggle-button {
    background-color: #4A5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    /*鼠标指向为手指型*/
    cursor: pointer;
  }
</style>
